﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace drawing_print.util
{
    class Download
    {
        public static bool HttpDownload_double(string url, string path)
        {
            if(HttpDownload( url, path))
            {
                return true;
            }
            else
            {
                if (HttpDownload(url, path))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
       
        public static bool download(string url, string path)
        {
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Method = "GET";
            try
            {
                

                using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                {
                    using (Stream responseStream = httpResponse.GetResponseStream())
                    {
                        using (FileStream localFileStream =new FileStream(path, FileMode.Create))
                        {
                            var buffer = new byte[4096];
                            long totalBytesRead = 0;
                            int bytesRead;

                            while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                totalBytesRead += bytesRead;
                                localFileStream.Write(buffer, 0, bytesRead);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw;
                return false;
                
            }
            return true;
        }
        public static bool HttpDownload(string url, string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);    //存在则删除
            }
            FileStream fs = null;
            Stream responseStream = null;
            try
            {
                fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                // 设置参数
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                //发送请求并获取相应回应数据
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                responseStream = response.GetResponseStream();
                //创建本地文件写入流
                byte[] bArr = new byte[1024];
                //当前长度
                long totalDownloadedByte = 0;
                //总长度
                long totalBytes = response.ContentLength;
                int size = responseStream.Read(bArr, 0, (int)bArr.Length);
                while (size > 0)
                {
                    fs.Write(bArr, 0, size);
                    size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    totalDownloadedByte += size;
                    float percent = (float)totalDownloadedByte / (float)totalBytes * 100;
                    percent = percent * 0.4f + 10;
                    int value = Convert.ToInt32(percent);
                }
                
                
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if(fs != null)
                {
                    fs.Close();
                }
                if (responseStream != null )
                {
                    responseStream.Close();
                }
                
            }
        }
    }
}
